package indi.gao;

import indi.gao.entity.UploadOralLog;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c)2020$</p>
 * <p>Company: Tope</p>
 * <P>Created Date :2020$-02$-29$</P>
 * <P>@version 1.0</P>
 */
public class InitClassNotNull {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        UploadOralLog o = (UploadOralLog) newClassNotNull(UploadOralLog.class);
        System.out.println(o.toString());
    }

    public static Object newClassNotNull(Class<?> Bean) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        List<Field> fieldList = new ArrayList<>();
        Object object = Bean.newInstance();
        while (Bean != null) {
            fieldList.addAll(Arrays.asList(Bean.getDeclaredFields()));
            Bean = Bean.getSuperclass();
        }
        Field[] fields2 = new Field[fieldList.size()];
        Field[] fields3 = fieldList.toArray(fields2);
        for (Field field : fields3) {
            int modifiers = field.getModifiers();
            if (modifiers == 25) {
                continue;
            }
            // 给所有字段初始化
            field.setAccessible(true);
            switch (field.getType().getName()) {
                case "java.lang.String":
                    field.set(object, " ");
                    break;
                case "java.lang.Integer":
                    field.set(object, 0);
                    break;
                case "java.lang.Boolean":
                    field.set(object, false);
                    break;
                case "java.lang.Long":
                    field.set(object, 0L);
                    break;
                case "java.lang.Byte":
                    field.set(object, 0);
                    break;
                case "java.lang.Short":
                    field.set(object, 0);
                    break;
                case "java.lang.float":
                    field.set(object, 0F);
                    break;
                case "java.lang.Double":
                    field.set(object, 0D);
                    break;
                case "java.lang.Character":
                    field.set(object, ' ');
                    break;
                case "java.util.Date":
                    Object o = Class.forName("java.util.Date").newInstance();
                    field.set(object, o);
                default:
                    break;
            }
        }
        return object;
    }
}
